﻿using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet
{
    class CellFormattingRecord : FormattingRecord
    {
        internal CellFormattingRecord()
        {
            
        }
        private StyleFormattingRecord _parent;
#if DEBUG
        public int internalId = counter++;
        private static int counter;
        public override string ToString()
        {
            return "" + GetHashCode() + this.FormatCode + " " +  this.Font + " "+ (_parent != null?Parent.ToString():"") + this.IsHidden + this.IsLocked + this.Background + this.Foreground + this.Borders + this.TextWrapping + this.TextRotation + this.ApplyFont   ;
        }

#endif

        public CellFormattingRecord Clone()
        {
            Contract.Ensures(Contract.Result<CellFormattingRecord>() != null);
            Contract.Ensures(Contract.Result<CellFormattingRecord>().Equals(this));
            var result = (CellFormattingRecord)MemberwiseClone();
            result.CopyFrom(this);
            return result;
        }
        internal CellFormattingRecord CloneFor (Workbook workbook)
        {
            var result = Clone();
            if (this._parent != null)
            {
                StyleFormattingRecord record = _parent;
                var style = record.Style;
                CellStyle newStyle;
                if (style.IsBuiltIn)
                {
                    newStyle = Enumerable.First(workbook.StyleSheet.BuiltInStylesNew,
                                                    s => s.BuiltInId == style.BuiltInId);
                }
                else
                {
                    newStyle = Enumerable.FirstOrDefault(workbook.StyleSheet.BuiltInStylesNew,
                                                    s => s.Name == style.Name);
                    if (newStyle != null)
                    {
                    //    result.SetParent(newStyle.Record);
                    }
                    else
                    {
                        newStyle = new CellStyle(style.Name);
                        newStyle.Record.CopyFrom(_parent);
                      //  result.SetParent(newStyle.Record);
                    }
                }
                result.SetParent(newStyle.Record);

            }
            return result;
        }
        public override bool Equals(object obj)
        {
            if (obj is CellFormattingRecord)
                return EqualsRecord((CellFormattingRecord   )obj);
            return false;
        }


        public void SetParent(StyleFormattingRecord record)
        {
            Contract.Ensures(record == Parent);
            _parent = record;
        }

        public override void CopyFrom (FormattingRecord other)
        {
            base.CopyFrom(other);
            if (other is CellFormattingRecord)
            this._parent = ((CellFormattingRecord)other)._parent;
        }
        public override StyleFormattingRecord Parent
        {
            get { return _parent; }
        }
    }
}